<template>
  <div class="recharge-records-container">
    <!-- 返回按钮 -->
    <div class="back-btn">
      <el-button type="primary" @click="goBack">返回我</el-button>
    </div>

    <h2 class="title">储值记录</h2>

    <el-table
      :data="rechargeList"
      border
      style="width: 100%"
      :header-cell-style="{ background: '#f5f7fa', color: '#333' }"
      :row-style="{ height: '50px' }"
    >
      <el-table-column
        prop="id"
        label="订单号"
        align="center"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="金额"
        align="center"
        width="120"
      >
        <template #default="{ row }">
          <span style="color: #ff6700; font-weight: bold">¥{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        width="200"
      >
        <template #default="{ row }">
          <span style="color: #666">{{ formatTime(row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { getRechargeOrders } from '@/api/me'

const router = useRouter()


// 格式化时间
const formatTime = (time) => {
  if (!time) return '--'
  const date = new Date(time)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}.${month}.${day} ${hours}:${minutes}:${seconds}`
}

// 返回“我”页面
const goBack = () => {
  router.back() // 或者使用 router.push('/me')
}
const rechargeList = ref([])
 onMounted(async () => {
   try {
    const res = await getRechargeOrders()
     if (res.code === 1) {
       rechargeList.value = res.data || []
     } else {
       ElMessage.error(res.msg || '获取储值记录失败')
     }
   } catch (error) {
      ElMessage.error('网络错误，请稍后再试')
     console.error('获取储值记录失败:', error)
   }
 })
</script>

<style scoped>
.recharge-records-container {
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.back-btn {
  margin-bottom: 20px;
}

.title {
  text-align: center;
  color: #333;
  font-size: 24px;
  margin-bottom: 20px;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}
</style>